/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin			*/
/*	Project:	GlbCloud			*/
/*	Author:		gong_libin			*/
/*	Date:		2013_05_16			*/
/*	File:		MrTmap.h			*/
/********************************************************/

#ifndef _MRTMAP_H
#define	_MRTMAP_H

#include "MrMap.h"
#include "MrRwLock.h"

#include <iostream>
#include <string>
#include <map>

template<typename K, typename V, typename C>
class CMrTmap : public CMrMap<K, V, C>
{
public:
	CMrTmap() {};
	virtual ~CMrTmap() {};

	void MrTmapDisplay();
	void MrTmapDestroy();
	int MrTmapCreateLock();
	void MrTmapDeleteLock();

	int MrTmapUnlock();
	int MrTmapReadLock();
	int MrTmapWriteLock();

	inline void MrTmapDelete(K);
	inline bool MrTmapInsert(K, V&);
	inline bool MrTmapUpdate(K, V&);
	inline bool MrTmapSelect(K, V&, int);

private:
	CMrRwLock m_CLock;

};

template<typename K, typename V, typename C>
int CMrTmap<K, V, C>::MrTmapCreateLock()
{
	if (NULL == m_CLock.MrLockGetLock()) {
		if (MR_SUCCESS == m_CLock.MrLockMalloc(NULL)) {
			if (MR_SUCCESS != m_CLock.MrLockCreate(false)) {
				return MR_FAILURE;
			}
		}
		else {
			return MR_FAILURE;
		}
	}

	return MR_SUCCESS;
}

template<typename K, typename V, typename C>
void CMrTmap<K, V, C>::MrTmapDeleteLock()
{
	if (NULL != m_CLock.MrLockGetLock()) {
		m_CLock.MrLockDelete();
		m_CLock.MrLockFree();
	}

	return;
}

template<typename K, typename V, typename C>
int CMrTmap<K, V, C>::MrTmapUnlock()
{
	return m_CLock.MrLockUnlock();
}

template<typename K, typename V, typename C>
int CMrTmap<K, V, C>::MrTmapReadLock()
{

	return m_CLock.MrLockLock(MR_RW_RDLK);
}

template<typename K, typename V, typename C>
int CMrTmap<K, V, C>::MrTmapWriteLock()
{
	return m_CLock.MrLockLock(MR_RW_WRLK);
}

template<typename K, typename V, typename C>
void CMrTmap<K, V, C>::MrTmapDestroy()
{
	if (NULL != m_CLock.MrLockGetLock()) {
		MrTmapWriteLock();
		CMrMap<K, V, C>::MrMapDestroy();
		MrTmapUnlock();
	}

	return;
}

template<typename K, typename V, typename C>
void CMrTmap<K, V, C>::MrTmapDisplay()
{
	if (NULL != m_CLock.MrLockGetLock()) {
		MrTmapReadLock();
		CMrMap<K, V, C>::MrMapDisplay();
		MrTmapUnlock();
	}

	return;
}

template<typename K, typename V, typename C>
inline void CMrTmap<K, V, C>::MrTmapDelete(K Key)
{
	if (NULL != m_CLock.MrLockGetLock()) {
		MrTmapWriteLock();
		CMrMap<K, V, C>::MrMapDelete(Key);
		MrTmapUnlock();
	}

	return;
}

template<typename K, typename V, typename C>
inline bool CMrTmap<K, V, C>::MrTmapInsert(K Key, V& Value)
{
	bool bReturn = true;

	if (NULL != m_CLock.MrLockGetLock()) {
		MrTmapWriteLock();
		bReturn = CMrMap<K, V, C>::MrMapInsert(Key, Value);
		MrTmapUnlock();
	}

	return bReturn;
}

template<typename K, typename V, typename C>
inline bool CMrTmap<K, V, C>::MrTmapUpdate(K Key, V& Value)
{
	bool bReturn = true;

	if (NULL != m_CLock.MrLockGetLock()) {
		MrTmapWriteLock();
		CMrMap<K, V, C>::MrMapDelete(Key);
		bReturn = CMrMap<K, V, C>::MrMapInsert(Key, Value);
		MrTmapUnlock();
	}

	return bReturn;
}

template<typename K, typename V, typename C>
inline bool CMrTmap<K, V, C>::MrTmapSelect(K Key, V& Value, int iSize)
{
	bool bReturn = true;

	if (NULL != m_CLock.MrLockGetLock()) {
		MrTmapReadLock();
		bReturn = CMrMap<K, V, C>::MrMapSelect(Key, Value, iSize);
		MrTmapUnlock();
	}

	return bReturn;
}

#endif /* _MRTMAP_H */
